<?php

namespace addons\member\model;

/**
 * Description of TradingRecord
 *
 * @author shilinqing
 */
class TradingRecord extends \web\common\model\BaseModel{
    
    protected function _initialize() {
        $this->tableName = 'trading_record';
    }


    /**
     * 前台交易记录列表
     * @param int $pageIndex
     * @param int $pageSize
     * @param string $filter
     * @param string $fields
     * @param string $order
     * @return mixed
     */
    public function getTradingRecordList($pageIndex = -1, $pageSize = -1, $filter = '', $fields = 'a.*', $order = 'a.id desc') {

        $sql = 'select '.$fields.' from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id left join tp_member_account c on c.id=a.to_user_id left join tp_trading_record_type as trt on trt.id=a.type left join tp_sys_balance_conf sb on sb.type=a.asset_type ';
        if (!empty($filter))
            $sql .= 'where ' . $filter;
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }

     /**
     * 获取记录总数
     * @param type $filter
     * @return int
     */
    public function getTradingRecordTotal($filter = '') {
        $sql = 'select count(*) as c from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id left join tp_member_account c on c.id=a.to_user_id left join tp_trading_record_type as trt on trt.id=a.type left join tp_sys_balance_conf sb on sb.type=a.asset_type ';
        if (!empty($filter))
            $sql .= 'where ' . $filter;
        $result = $this->query($sql);

        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }

    public function getCountTradingRecordTotal($filter = '') {
        $sql = 'select  sum(amount) as count_total from '.$this->getTableName() .' a left join tp_member_account b on b.id=a.user_id left join tp_member_account c on c.id=a.to_user_id left join tp_trading_record_type as trt on trt.id=a.type left join tp_sys_balance_conf sb on sb.type=a.asset_type ';
        if (!empty($filter))
            $sql .= 'where ' . $filter;
        $count = $this->query($sql);
        return $count[0]['count_total'];
    }
    /**
     * 添加记录
     * @param type $user_id 用户id
     * @param type $amount 数量
     * @param type $before_amount  更新前数量
     * @param type $after_amount   更新后数量
     * @param type $asset_type 资产类型：1=USDT 2-元气豆
     * @param int $type        记录类型：
     * @param int $change_type 0 = 减少 ；1 = 增加
     * @param int $to_user_id      目标用户
     * @param string $remark      备注
     * @param string $otc_price      otc实时价 美金
     * @param string $otc_cny_price     otc实时价 ￥
     * @return type
     */
    public function addRecord($user_id,$amount, $before_amount, $after_amount,$asset_type, $type, $change_type=0, $to_user_id = 0, $remark='',$static=1,$otc_price='',$otc_cny_price='',$is_poundage=0){
        if($amount<=0) return false;
        $data['user_id'] = $user_id;
        $data['to_user_id'] = $to_user_id;
        $data['asset_type'] = $asset_type;
        $data['type'] = $type; 
        $data['change_type'] = $change_type;
        $data['before_amount'] = $before_amount;
        $data['after_amount'] = $after_amount;
        $data['amount'] = $amount;
        $data['remark'] = $remark;
        $data['static'] = $static;
        $data['update_time'] = NOW_DATETIME;
        $data['otc_price'] = $otc_price;
        $data['otc_cny_price'] = $otc_cny_price;
        $data['is_poundage'] = $is_poundage;
        return $this->add($data);
    }

    public function getList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
        $coinM = new \addons\config\model\Coins();
        $userM = new \addons\member\model\MemberAccountModel();
        $recordConfM  = new \addons\member\model\RecordConf();
        $sql = 'select a.*,c.coin_name,d.trade_type from '.$this->getTableName() . ' a,'.$coinM->getTableName().' c,'.$recordConfM->getTableName().' d where a.coin_id=c.id and a.type = d.id';
        $sql = 'select s.*,u.username from ('.$sql.') as s left join '.$userM->getTableName().' u on s.user_id=u.id';
        if($filter != ''){
            $sql = 'select '.$fileds.' from ('.$sql.') as tab where '.$filter;
        }
        $sql = 'select t.*,p.username to_username from ('.$sql.') as t left join '.$userM->getTableName().' p on t.to_user_id=p.id';
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }
    
    public function getDataList($pageIndex = -1, $pageSize = -1, $filter = '',$fileds='*', $order = 'id desc') {
        $c = new \addons\config\model\BalanceConf();
        $userM = new \addons\member\model\MemberAccountModel();
        $sql = 'select a.*,c.name from '.$this->getTableName() . ' a,'.$c->getTableName().' c where a.asset_type=c.id';
        $sql = 'select '.$fileds.' from ('.$sql.') as tab';
        $sql = 'select s.*,u.username,u.phone as u_phone,t.username as to_username from ('.$sql.') as s left join '.$userM->getTableName().' u on s.user_id=u.id left join '.$userM->getTableName().' t on s.to_user_id=t.id';
        if($filter != ''){
            $sql = 'select * from ('.$sql.') as y where '.$filter;
        }
        return $this->getDataListBySQL($sql, $pageIndex, $pageSize, $order);
    }
    
    /**
     * 获取记录总数
     * @param type $filter
     * @return int
     */
    public function getTotal($filter = '') {
        $c = new \addons\config\model\BalanceConf();
        $userM = new \addons\member\model\MemberAccountModel();
        $sql = 'select a.*,c.name from '.$this->getTableName() . ' a,'.$c->getTableName().' c where a.asset_type=c.id';
        $sql = 'select s.*,u.username,u.phone as u_phone from ('.$sql.') as s left join '.$userM->getTableName().' u on s.user_id=u.id';
        if($filter != ''){
            $sql = 'select count(*) c from ('.$sql.') as y where '.$filter;
        }else{
            $sql = 'select count(*) c from ('.$sql.')';
        }
        $result = $this->query($sql);
        if (count($result) > 0)
            return intval($result[0]['c']);
        else
            return 0;
    }
    
    public function getCountTotal($filter = '') {
        $c = new \addons\config\model\BalanceConf();
        $userM = new \addons\member\model\MemberAccountModel();
        $sql = 'select a.*,c.name from '.$this->getTableName() . ' a,'.$c->getTableName().' c where a.asset_type=c.id';
        $sql = 'select s.*,u.username,u.phone as u_phone from ('.$sql.') as s left join '.$userM->getTableName().' u on s.user_id=u.id';
        if($filter != ''){
            $sql = 'select sum(amount) as count_total from ('.$sql.') as y where '.$filter;
        }else{
            $sql = 'select sum(amount) as count_total from ('.$sql.')';
        }
        $count = $this->query($sql);
        return $count[0]['count_total'];
    }
    
    /**
     * 根据类型获取记录总额
     * @param type $type
     * @return type
     */
    public function getTotalByMultiType($type='4,5,6',$start_time='',$end_time=''){
        if($start_time != '')
            $where['update_time'] = array(">=",$start_time);
        if($end_time != ''){
            $where1['update_time'] = array("<=",$end_time);
            $this->where($where1);
        }
        $where['type'] = array('in',$type);
        $total = $this->where($where)->sum('amount');
        if(empty($total)){
            return 0;
        }else{
            return $total;
        }
        
    }
    
    /**
     * 获取用户工资发放记录
     * @param type $user_id
     */
    public function getWageRecordID($user_id, $type, $start_time){
        $where['user_id'] = $user_id;
        $where['type'] = $type;
        $where['update_time'] = array('>',$start_time);
        return $this->where($where)->value('id');
    }
    
}
